//有效期时间插件设置
laydate.render({
    elem: '#expireTime',
    type:'datetime'
});

var $table = $('#addTo-index-goods');

$(function () {
});


//删除行
function delh(th) {
    // $(this).percent().html();
    //alert($(th).parent().parent().html());
    $(th).parent().parent().html("");
}

//新增行
function addh() {
    var html = "<tr>\n" +
        "<td><input name='options' class=\"form-control\" style=\"width: 50px\"></td>\n" +
        "<td><input name='optionContent' class=\"form-control\" style=\"width: 500px\"></td>\n" +
        "<td><button class=\" btn btn-danger\" onclick=\"delh(this)\" style=\"width: 50px\">删除</button></td>\n" +
        "</tr>";

    $("#opTable").append(html);
}

var cont = 0;

//保存
function add() {

    if ($("#title").val() == "") {
        alert("问卷标题不能为空！");
        return false;
    }
    if ($("#content").val() == "") {
        alert("问题不能为空！");
        return false;
    }

    var type = $("#type").val();//问卷类型
    var bonus = $("#bonus").val();//奖励
    var expireTime=$("#expireTime").val();
    if (type == 2 ) {
        if(bonus == ""){
            alert("问卷奖励不能为空");
            return;
        }
        if(expireTime==""){
            alert("有奖问卷有效期不能为空");
        }
    }else expireTime=null;


    var flag = true;
    var options = "";

    var o = [];
    var c = [];
    $("[name='options']").each(function (i) {
        o.push($(this).val());
        if ($(this).val() == "") {
            alert("选项不能为空！");
            flag = false;
        }
        options += $(this).val() + ",";
    });
    var content = "";
    $("[name='optionContent']").each(function (i) {
        c.push($(this).val());
        if ($(this).val() == "") {
            alert("内容不能为空！");
            flag = false;
        }
        content += $(this).val() + ",";
    });
    if (flag) {
        $.ajax({
            type: "POST",
            url: urlcore + "/api/questionnaire/add",
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify({
                naireId: $("#naireId").val(),
                naireTitle: $("#title").val(),
                bonus: bonus,
                expireTime:expireTime,
                type: type,
                questionType: $("#groupType").val(),
                options: options,
                optionContent: content,
                questionTitle: $("#content").val()
            }),
            // data:$("#myForm").serialize(),
            success: function (data) {
                if (data.code == "SUCCESS") {
                    //  alert("当前问题保存成功！");
                    $("#naireId").val(data.data);
                    ++cont;

                    var html = "<tr>\n" +
                        "<td>问题</td>\n" +
                        "<td>类型</td>\n" +
                        "</tr>";
                    var lex = "";
                    if ($("#groupType").val() == 1) {
                        lex = "单选";
                    } else {
                        lex = "多选";
                    }

                    var thrHtml = "";
                    $("input[name='options']").each(function (i, v) {

                        thrHtml += "<td>" + $(this).val() + "、" + c[i] + "</td>";
                    });

                    html += "<tr>\n" +
                        "<td>" + (cont) + "、" + $("#content").val() + "</td>\n" +
                        "<td>" + lex + "</td>\n" +
                        "</tr>\n" +
                        "<tr>\n" + thrHtml +
                        "</tr>";
                    $("#opTables").append(html);

                    //清除页面数据
                    $("#content").val("");
                    $("[name='options']").each(function (i) {
                        $(this).val("");
                    });
                    $("[name='optionContent']").each(function (i) {
                        $(this).val("");
                    });


                } else {
                    alert("添加失败");
                }

            },
            error: function () {
                alert("添加失败");
            }
        });
    }
}

//问卷类型变为用户普通问卷显示bonus和expireTime
function changeType(typeValue) {
    if(typeValue==2){
        $("#bonusDiv").show();
        $("#expireDateDiv").show();
    }else {
        $("#bonusDiv").hide();
        $("#expireDateDiv").hide();
    }

}

